package com.showcai.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.showcai.dao.CertificateBatchMapper;
import com.showcai.module.CertificateBatch;
import com.showcai.module.CertificateClassify;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.service.CertificateBatchService;
import com.showcai.service.CertificateClassifyService;
import com.showcai.service.CertificateService;
import com.showcai.service.ServiceBase;
import com.showcai.util.GenerateID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by zpc on 2017/7/11.
 */
@Service
public class CertificateBatchServiceImpl extends ServiceBase implements CertificateBatchService {
    @Autowired
    CertificateBatchMapper certificateBatchMapper;
    @Autowired
    CertificateService certificateService;
    @Autowired
    CertificateClassifyService certificateClassifyService;

    @Override
    public CertificateBatch getCertificateBathByBatchId(Long batchId) {
        return certificateBatchMapper.getCertificateBathByBatchId(batchId);
    }

    @Override
    public List<CertificateBatch> getCertificateBatchList(Map<String, String> map) throws MyRunTimeExcption {
        return certificateBatchMapper.getCertificateBatchList(map);
    }

    @Override
    public CertificateBatch createCertificateBatch(CertificateBatch certificateBatch) throws MyRunTimeExcption {
        certificateBatch.setCreateTime(new Date());
        certificateBatch.setBatchId(GenerateID.CreatetID());
        certificateBatch.setStatus(CertificateBatch.STATUS_VALID);
        certificateBatchMapper.insertCertificateBatch(certificateBatch);
        CertificateClassify certificateClassify = certificateClassifyService.selectCertificateClassifyById(certificateBatch.getCertificateClassifyId());
        if (certificateClassify.getCertificateCreateType() == CertificateClassify.CREATE_TYPE_NOW) {
            certificateService.createCertificate(certificateBatch);
        }


        return certificateBatch;
    }

    @Override
    public CertificateBatch modifyCertificateBatch(CertificateBatch certificateBatch) throws MyRunTimeExcption {
        if (certificateBatch.getTid() == null) {

            createCertificateBatch(certificateBatch);
            return certificateBatch;
        }
//TODO 需要更新卡券的具体信息系主要是时间
        certificateBatchMapper.updateCertificateBatchByPrimaryKeySelective(certificateBatch);
        return certificateBatch;
    }

    @Override
    public PageInfo getPageInfoCertificateBatchList(Map<String, String> map) {
        Integer page = Integer.parseInt(map.get("page"));
        Integer pageSize = Integer.parseInt(map.get("pageSize"));
        PageHelper.startPage(page, pageSize);
        List<CertificateBatch> certificateBatchList = certificateBatchMapper.getCertificateBatchList(map);
        PageInfo<CertificateBatch> pageinfo = new PageInfo<>(certificateBatchList);
        return pageinfo;
    }
}
